package czy.demo.controller.example;

import czy.demo.pojo.Person;
import czy.demo.repository.PersonRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.repository.query.QueryByExampleExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import static org.springframework.data.domain.ExampleMatcher.GenericPropertyMatcher.of;


@RestController
@RequestMapping("example")
@Api(value = "Example查询",tags = "Example查询")
public class ExampleController implements InitializingBean {

    private static final Logger logger = LoggerFactory.getLogger(ExampleController.class);

    private static final ExampleMatcher ALL = ExampleMatcher
            .matchingAll().withIgnoreNullValues();
    private static final ExampleMatcher ANY = ExampleMatcher
            .matchingAny().withIgnoreNullValues();
    private static final ExampleMatcher BY_ID = ExampleMatcher
            .matchingAny().withIgnoreNullValues().withIgnoreCase("name","age","birthday");
    private static final ExampleMatcher BY_NAME_END_WITH = ExampleMatcher
            .matchingAny().withIgnoreNullValues().withIgnoreCase("id","age","birthday").withMatcher("name", ExampleMatcher.GenericPropertyMatcher.of(ExampleMatcher.StringMatcher.ENDING));
    private static final ExampleMatcher BY_NAME_START_WITH = ExampleMatcher
            .matchingAny().withIgnoreNullValues().withIgnoreCase("id","age","birthday").withMatcher("name", ExampleMatcher.GenericPropertyMatcher.of(ExampleMatcher.StringMatcher.STARTING));
    private static final ExampleMatcher BY_NAME_CONTAIN = ExampleMatcher
            .matchingAny().withIgnoreNullValues().withIgnoreCase("id","age","birthday").withMatcher("name", ExampleMatcher.GenericPropertyMatcher.of(ExampleMatcher.StringMatcher.CONTAINING));
    private static final ExampleMatcher BY_NAME_REGEX = ExampleMatcher
            .matchingAny().withIgnoreNullValues().withIgnoreCase("id","age","birthday").withMatcher("name", ExampleMatcher.GenericPropertyMatcher.of(ExampleMatcher.StringMatcher.REGEX));

    @Autowired
    private PersonRepository personRepository;

    /* example查询接口 */
    private QueryByExampleExecutor<Person> exampleExecutor;

    @Override
    public void afterPropertiesSet() throws Exception {
        this.exampleExecutor = personRepository;
    }

    @GetMapping("/all")
    @ApiOperation("单个实例example查询，所有字段需要相等")
    public Person all(Person person){
        Example<Person> example = Example.of(person,ALL);
        return this.exampleExecutor.findOne(example).get();
    }

    @GetMapping("/any")
    @ApiOperation("单个实例example查询，字段之一相等即可")
    public Person any(Person person){
        Example<Person> example = Example.of(person,ANY);
        return this.exampleExecutor.findOne(example).get();
    }

    @GetMapping("/id")
    @ApiOperation("通过id查询单个实例")
    public Person findById(Person person){
        Example<Person> example = Example.of(person,BY_ID);
        return this.exampleExecutor.findOne(example).get();
    }

    @GetMapping("/name/startwith")
    @ApiOperation("查询name以指定字符串结束的实例")
    public Person findByNameStartWith(Person person){
        Example<Person> example = Example.of(person,BY_NAME_START_WITH);
        return this.exampleExecutor.findOne(example).get();
    }

    @GetMapping("/name/endwith")
    @ApiOperation("查询name以指定字符串结束的实例")
    public Person findByNameEndWith(Person person){
        Example<Person> example = Example.of(person,BY_NAME_END_WITH);
        return this.exampleExecutor.findOne(example).get();
    }

    @GetMapping("/name/contain")
    @ApiOperation("查询name以指定字符串结束的实例")
    public Person findByNameContain(Person person){
        Example<Person> example = Example.of(person,BY_NAME_CONTAIN);
        return this.exampleExecutor.findOne(example).get();
    }

    @GetMapping("/name/regex")
    @ApiOperation("查询name以指定字符串结束的实例")
    public Person findByNameRegex(Person person){
        Example<Person> example = Example.of(person,BY_NAME_REGEX);
        return this.exampleExecutor.findOne(example).get();
    }
}
